public with sharing class trg_OpportunityMethods
{
	public static void InsertKentechJobOnOpportunity(Map<Id, Opportunity> opportunityMap)
	{
		Set<Id> accountIds = new Set<Id>();
		Map<Id, Project_Account__c> projectAccounts = new Map<Id, Project_Account__c>();
		
		// get opportunities where there is no Kentech Job allocated
		for (Opportunity opportunity : [
			SELECT
				Id,
				AccountId,
				(
					SELECT
						Id
					FROM
						Job_Creations__r)
			FROM
				Opportunity
			WHERE
				Id IN :opportunityMap.keySet()])
		{
			if (opportunity.Job_Creations__r == null || opportunity.Job_Creations__r.isEmpty())
			{
				accountIds.add(opportunity.AccountId);
			}
		}

		// Select appropriate Project Account for the opportunity
		for (Project_Account__c projectAccount : [
			SELECT
				Project__c,
				Account__c
			FROM
				Project_Account__c
			WHERE
				Account__c IN :accountIds])
		{
			projectaccounts.put(projectAccount.Account__c, projectAccount);
		}
		
		
		// If we have a valid project for the account, create a new kentech Job.
		List<Kentech_Job__c> kentechJobsCache = new List<Kentech_Job__c>();
		for (Opportunity opportunity : opportunityMap.values())
		{
			if (projectaccounts.containsKey(opportunity.AccountId))
			{
				Kentech_Job__c kentechJob = new Kentech_Job__c(
					Opportunity__c = opportunity.Id,
					Project__c = projectaccounts.get(opportunity.AccountId).Project__c);
				kentechJobsCache.add(kentechJob);
			}
		}
		insert kentechJobsCache;
	}
	///aaaaaaaaaaaaa
	
	public static testMethod void test_InsertKentechJobOnOpportunity()
	{
		Account account = new Account(
			Name = 'Test Account');
		insert account;
		
		Industrial_Cluster__c industrialCluster = new Industrial_Cluster__c();
		insert industrialCluster;
		
		Entity__c entity = new Entity__c(
			Industrial_Cluster__c = industrialCluster.Id);
		insert entity;
		
		Project__c project = new Project__c(
			Entity__c = entity.Id);
		insert project;
		
		Project_Account__c projectAccount = new Project_Account__c(
			Project__c = project.Id,
			Account__c = account.Id);
		insert projectAccount; 
		
		Opportunity opportunity = new Opportunity(
			Name = 'Test Opportunity',
			StageName = 'Prospect Pre-tender',
			CloseDate = Date.today().addDays(30),
			AccountId = account.Id);
		insert opportunity;
		
		opportunity.StageName = 'Closed Won';
		update opportunity;
		
		List<Kentech_Job__c> kentechJobs = getTestJobs(opportunity.Id);
		System.assertEquals(kentechJobs.size(), 1);
		System.assertEquals(kentechJobs.get(0).Project__c, project.Id);
		
		// Test context
		TriggerContext.DisableOpportunityTriggers = true;
		opportunity.StageName = 'Suspect';
		update opportunity;
		TriggerContext.DisableOpportunityTriggers = false;
		
		// Test opportunity creation
		opportunity = new Opportunity(
			Name = 'Test Opportunity',
			StageName = 'Closed Won',
			CloseDate = Date.today().addDays(40),
			AccountId = account.Id);
		insert opportunity;
		
		kentechJobs = getTestJobs(opportunity.Id);
		System.assertEquals(kentechJobs.size(), 1);	
	}
	
	private static List<Kentech_Job__c> getTestJobs(Id opportunityId)
	{
		return new List<Kentech_Job__c>([
			SELECT
				Id,
				Project__c
			FROM
				Kentech_Job__c
			WHERE
				Opportunity__c = :opportunityId]);
	}
}